package com.coderman.daotest.dao;

import com.coderman.daoclient.dao.impl.IDaoImpl;
import com.coderman.daotest.csdn.bean.CsdnUserEntity;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.*;

import static java.util.stream.Collectors.groupingBy;


/**
 * Created by 58-daojia on 2017-12-16.
 *
 * @author: fanchunshuai
 * @version: V1.0
 * @Desc:
 * @Copyright (c) 2017 58到家-版权所有
 */
public class IDaoImplTest2 {
    static IDaoImpl<CsdnUserEntity,Long> dao = new IDaoImpl<CsdnUserEntity,Long>();

    public static void main(String[] args) {
        try {
           /* List<CsdnUserEntity> csdnUserEntityList = dao.getList(CsdnUserEntity.class);
            File file = new File("E:\\spiderFile\\userID.txt");
            List<String> l = new ArrayList<>();
            csdnUserEntityList.stream().collect(groupingBy(CsdnUserEntity::getUserName))
            .forEach((k,v) -> {

                if(v.size() > 1){
                    Collections.sort(v);
                    l.add(v.get(0).getId()+"");
                }else {

                    l.add(v.get(0).getId()+"");
                }

            });*/

           /* try {
                FileUtils.writeLines(file,l);
            } catch (IOException e) {
                e.printStackTrace();
            }*/



            File file2 = new File("E:\\spiderFile\\userID.txt");
            Map<String,String> map = new HashMap<>();

            List<String> list = FileUtils.readLines(file2,"UTF-8");

            for(String x : list){
                map.put(x,x);
            }

            List<CsdnUserEntity> csdnUserEntityList = dao.getList(CsdnUserEntity.class);
            csdnUserEntityList.parallelStream().forEach((x)->{
                if(!map.containsKey(x.getId()+"")){
                    try {
                        dao.deleteEntity(CsdnUserEntity.class,x.getId());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });

        } catch (Exception e) {

            e.printStackTrace();
        }

    }
}
